package com.example.movie.ui

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment.findNavController
import androidx.navigation.ui.setupWithNavController
import com.example.movie.R
import com.example.movie.common.constants.Constants
import com.example.movie.data.user.login.factory.LoginViewModelFactory
import com.example.movie.data.user.login.model.SharedLoginViewModel
import com.example.movie.utils.ContextHolder
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    val loginViewModel: SharedLoginViewModel by viewModels(factoryProducer = { LoginViewModelFactory() })

    var location = -1

    override fun onCreate(savedInstanceState: Bundle?) {
        Log.i("debug", "onCreate: mainActivity已重新创建")
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bottom_nav.setupWithNavController(findNavController(nav_host_fragment))
        init()

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        //不设置actionBar
//        val appBarConfiguration = AppBarConfiguration(
//            setOf(
//                R.id.navigation_home, R.id.navigation_hotspot, R.id.navigation_order,R.id.navigation_user_center
//            )
//        )
//        setupActionBarWithNavController(navController, appBarConfiguration)

    }

    override fun onStart() {
        super.onStart()
        Log.i("debug", "onStart: mainActivity已开始")
    }

    override fun onResume() {
        super.onResume()
        Log.i("debug", "onPause: mainActivity已恢复")
        when (location) {
            Constants.JumpAction.toCinemaFragment -> {
                bottom_nav.selectedItemId = R.id.navigation_cinema
            }
            Constants.JumpAction.toUserFragment -> {
                bottom_nav.selectedItemId = R.id.navigation_user
            }
        }
    }

    override fun onPause() {
        super.onPause()
        Log.i("debug", "onPause: mainActivity已暂停")
    }

    override fun onStop() {
        super.onStop()
        Log.i("debug", "onPause: mainActivity已停止")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i("debug", "onDestroy: mainActivity已销毁")
    }


    private fun init() {
        ContextHolder.addContext(this)
        tryAutoLoginByToken()
    }

    /**
     * 应用启动的时候拿着token尝试自动登陆，如果token过期则提示登陆过期并且跳转到登陆页面
     */
    private fun tryAutoLoginByToken() {
        // TODO: 2021/1/3 还差一个登陆过期限制
        with(getSharedPreferences(getString(R.string.login_store_file_name),Context.MODE_PRIVATE)){
            val userName = this.getString(getString(R.string.login_username), null)
            val passWord = this.getString(getString(R.string.login_password), null)
            if (userName == null || passWord == null){
                //登陆已过期
                return
            }
            loginViewModel.login(userName,passWord)
        }

    }


}

